﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.EntityContext;

namespace GestionPedago.Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'Entity VersionECF
    /// </summary>
    public abstract class VersionECFOPManager
    {
        /// <summary>
        /// Méthode de récupération d'un objet VersionECF par son id
        /// </summary>
        /// <param name="idVersionECF">Identifiant de la versionECF</param>
        /// <returns>L'objet VersionECF</returns>
        public static VersionECF GetVersionECFByIdVersionECF(int idVersionECF)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().VersionECF
                            where it.IdVersionECF == idVersionECF
                            select it;

                VersionECF version = null;

                version = query.First();

                return version;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// Méthode de récupération d'une liste de versionECF par IdECF
        /// </summary>
        /// <param name="idECF">L'identifiant de l'ECF</param>
        /// <returns>La liste des versions ECF</returns>
        public static List<VersionECF> GetVersionECFByIdECF(int idECF)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().VersionECF
                            where it.IdECF == idECF
                            select it;

                return query.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        
        /// <summary>
        /// Méthode d'ajout d'une VersionECF
        /// </summary>
        /// <param name="vecf">L'entity VersionECF à ajouter</param>
        /// <returns>L'objet VersionECF</returns>
        public static VersionECF AjouterVersionECF(VersionECF vecf)
        {
            try
            {
                if (!EntityStateChecker.IsDetached(vecf))
                {
                    EntityConnection.GetContext().VersionECF.Detach(vecf);
                }
                EntityConnection.GetContext().VersionECF.AddObject(vecf);
                EntityConnection.GetContext().SaveChanges();
                return vecf;

            }
            catch (Exception ex)
            {

                throw ex;

            }
        }

        /// <summary>
        /// Méthode de suppression d'une VersionECF
        /// </summary>
        /// <param name="ve">L'objet VErsionECF à supprimer</param>
        public static void SupprimerVersionECF(VersionECF ve)
        {
            try
            {
                ve = VersionECFOPManager.GetVersionECFByIdVersionECF(ve.IdVersionECF);
                if (EntityStateChecker.IsDetached(ve))
                {
                    EntityConnection.GetContext().VersionECF.Attach(ve);
                }
                EntityConnection.GetContext().VersionECF.DeleteObject(ve);
                EntityConnection.GetContext().SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
